package com.boruan.qq.zxgylibrary.ui.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.boruan.qq.zxgylibrary.R;
import com.boruan.qq.zxgylibrary.service.manager.GXDrugModule;
import com.boruan.qq.zxgylibrary.service.model.InsuranceUrl;
import com.convenient.qd.core.base.arouter.ARouterConstant;
import com.convenient.qd.core.base.arouter.ARouterUtils;
import com.convenient.qd.core.bean.BaseResBean;
import com.convenient.qd.core.bean.MenuTab;
import com.convenient.qd.core.net.BaseHttpObserver;
import com.convenient.qd.core.utils.CommonUtils;
import com.convenient.qd.core.utils.ToastUtils;
import com.convenient.qd.core.widget.LoadingDiaLogUtils;

import java.text.DecimalFormat;

/**
 * Created by ren on 2020/3/5.
 */

public class DrugPayDialog extends Dialog {

    private TextView tv_ybpay_no_sign,tv_ybpay_ye;
    private LinearLayout ll_weiXin;
    private RelativeLayout ll_ybPay;
    private ImageView iv_weChat,iv_ybzf;
    private LinearLayout ll_aliPay;
    private ImageView iv_alipay;
    private View view_ybpay_musker,line_ybpay;
    private Activity activity;
    private LinearLayout ll_ylPay;
    private ImageView iv_ylpay;

    private Boolean isYBSign; // 是否签约医保余额支付
    private Boolean isYBPay; // 是否支持医保余额支付
    private Boolean isPayPwd; // 是否开通支付密码
    private Boolean isShowYBPay = false; // 是否显示余额支付
    private Long money;
    private String des; // 不支持医保支付原因
    private String payType;


    private OnPayClickListener onPayClickListener;

    public DrugPayDialog(@NonNull Context context, Long money,Boolean isYBSign,Boolean isYBPay,String des) {
        super(context, R.style.NoFunctionStyle);
        activity = (Activity) context;
        this.isYBSign = isYBSign==null?false:isYBSign;
        this.money = money;
        this.isYBPay = isYBPay==null?false:isYBPay;
        this.des = des;
    }

    public void setState(){
        if(isShowYBPay){
            line_ybpay.setVisibility(View.VISIBLE);
            ll_ybPay.setVisibility(View.VISIBLE);

            ll_ybPay.setEnabled(true);
            tv_ybpay_no_sign.setVisibility(View.VISIBLE);
            tv_ybpay_ye.setVisibility(View.GONE);
            view_ybpay_musker.setVisibility(View.GONE);
            iv_ybzf.setImageResource(R.mipmap.drug_icon_enter);
            if(this.isYBPay){
                if(this.isYBSign){
                    tv_ybpay_no_sign.setVisibility(View.GONE);
                    tv_ybpay_ye.setVisibility(View.VISIBLE);
                    iv_ybzf.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                    if(this.isYBPay){
                        if(!TextUtils.isEmpty(this.payType) && this.payType.equals("YBPAY")){
                            iv_ybzf.setImageResource(R.mipmap.drug_icon_tongyixieyi_pre);
                        }
                        tv_ybpay_ye.setText("已签约 ，个人账户余额："+format(money)+"元");
                    }else {
                        ll_ybPay.setEnabled(false);
                        view_ybpay_musker.setVisibility(View.VISIBLE);
                        tv_ybpay_ye.setText(this.des);
                    }

                }
            }else {
                tv_ybpay_no_sign.setVisibility(View.GONE);
                tv_ybpay_ye.setVisibility(View.VISIBLE);
                iv_ybzf.setImageResource(R.mipmap.drug_icon_tongyixieyi);

                ll_ybPay.setEnabled(false);
                view_ybpay_musker.setVisibility(View.VISIBLE);
                tv_ybpay_ye.setText(this.des);
            }
        }else {
            line_ybpay.setVisibility(View.GONE);
            ll_ybPay.setVisibility(View.GONE);
        }

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = LayoutInflater.from(getContext()).inflate(R.layout.drug_pay_dialog, (ViewGroup) getWindow().getDecorView(), false);
        setContentView(view);

        ll_weiXin = view.findViewById(R.id.ll_weiXin);
        iv_weChat = view.findViewById(R.id.iv_weChat);
        ll_aliPay = view.findViewById(R.id.ll_aliPay);
        iv_alipay = view.findViewById(R.id.iv_alipay);
        ll_ybPay = view.findViewById(R.id.ll_ybPay);
        tv_ybpay_no_sign = view.findViewById(R.id.tv_ybpay_no_sign);
        tv_ybpay_ye = view.findViewById(R.id.tv_ybpay_ye);
        iv_ybzf = view.findViewById(R.id.iv_ybzf);
        view_ybpay_musker = view.findViewById(R.id.view_ybpay_musker);
        line_ybpay= view.findViewById(R.id.line_ybpay);
        ll_ylPay= view.findViewById(R.id.ll_ylPay);
        iv_ylpay= view.findViewById(R.id.iv_ylpay);

        setState();

        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setLayout(CommonUtils.getScreenWidth(getContext()), WindowManager.LayoutParams.WRAP_CONTENT);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        setCanceledOnTouchOutside(true);

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                CommonUtils.backgroundAlpha(activity, 1f);
            }
        });

        setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                CommonUtils.backgroundAlpha(activity, 0.5f);
            }
        });


        ll_weiXin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv_ylpay.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                iv_weChat.setImageResource(R.mipmap.drug_icon_tongyixieyi_pre);
                iv_alipay.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                if(isYBSign){
                    iv_ybzf.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                }
                if(onPayClickListener != null){
                    onPayClickListener.onClickPay("WXPAY_APP","微信");
                }
                dismiss();
            }
        });

        ll_aliPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv_ylpay.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                iv_weChat.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                iv_alipay.setImageResource(R.mipmap.drug_icon_tongyixieyi_pre);
                if(isYBSign){
                    iv_ybzf.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                }
                if(onPayClickListener != null){
                    onPayClickListener.onClickPay("ALIPAY_PSCAN","支付宝");
                }
                dismiss();
            }
        });

        ll_ybPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv_ylpay.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                iv_weChat.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                iv_alipay.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                if(isYBSign){
                    iv_ybzf.setImageResource(R.mipmap.drug_icon_tongyixieyi_pre);
                }
                if(onPayClickListener != null){
                        if(isYBSign && isYBPay){
                            onPayClickListener.onClickPay("YBPAY","医保");
                            dismiss();
                        }else {
                            onPayClickListener.onClickPay("","请选择");
//                            ToastUtils.showShort("暂不支持医保余额支付，请选择其它支付方式");
                        }

                }
            }
        });

        ll_ylPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv_ylpay.setImageResource(R.mipmap.drug_icon_tongyixieyi_pre);
                iv_weChat.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                iv_alipay.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                if(isYBSign){
                    iv_ybzf.setImageResource(R.mipmap.drug_icon_tongyixieyi);
                }
                if(onPayClickListener != null){
                    onPayClickListener.onClickPay("CLOUDPAY","银联云闪付");
                }
                dismiss();
            }
        });

        tv_ybpay_no_sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                LoadingDiaLogUtils.showLoadingDialog(activity);
                GXDrugModule.getInstance().getInsuranceRegisterUrl(new BaseHttpObserver<BaseResBean<InsuranceUrl>>() {
                    @Override
                    public void onSuccess(BaseResBean<InsuranceUrl> s) {
                        if(s.getResult().getContent().equals("0000")){
                            if(isYBPay){
                                onPayClickListener.onClickPay("YBPAY","医保");
                                dismiss();
                            }
                            return;
                        }
                        MenuTab menuTab = new MenuTab();
                        menuTab.setAppStartUrl(s.getResult().getContent());
                        menuTab.setAppName("医保余额签约");
                        menuTab.setAddToorbar(1);
                        if(isPayPwd){
                            menuTab.setAndroidGoPage("");
                        }else {
                            menuTab.setAndroidGoPage(ARouterConstant.ACTIVITY_USER_PWD_SET_ACTIVITY);
                        }

                        Bundle bundle = new Bundle();
                        bundle.putSerializable("menuTab",menuTab);
                        ARouterUtils.navigation(ARouterConstant.ACTIVITY_COMMON_WEB,bundle);
                    }

                    @Override
                    public void onFailure(int status, String msg) {
                        ToastUtils.showShort(msg);
                    }
                });
            }
        });
    }

    public OnPayClickListener getOnPayClickListener() {
        return onPayClickListener;
    }

    public void setOnPayClickListener(OnPayClickListener onPayClickListener) {
        this.onPayClickListener = onPayClickListener;
    }

    public interface OnPayClickListener {
        void onClickPay(String payType,String payText);
    }

    public Boolean getPayPwd() {
        return isPayPwd;
    }

    public void setPayPwd(Boolean payPwd) {
        isPayPwd = payPwd;
    }

    public Boolean getYBSign() {
        return isYBSign;
    }

    public void setYBSign(Boolean YBSign) {
        isYBSign = YBSign;
    }

    public Boolean getYBPay() {
        return isYBPay;
    }

    public void setYBPay(Boolean YBPay) {
        isYBPay = YBPay;
    }

    public Long getMoney() {
        return money;
    }

    public void setMoney(Long money) {
        this.money = money;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    public String getPayType() {
        return payType;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }

    public Boolean getShowYBPay() {
        return isShowYBPay;
    }

    public void setShowYBPay(Boolean showYBPay) {
        isShowYBPay = showYBPay;
    }

    /**
     *
     * @param f 单位 分
     * @return
     */
    public static String format(Long f) {
        if(f==0)return "0.00";
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(f*0.01);
    }

//    public static void main(String[] args) {
//        System.out.println(format(619708L));
//    }

}
